<?
	include_once('header.php');

	include_once('sidebar.php');

	$process = $_POST['process'];
	extract($_GET);
	if(!$offset) { $offset = "0"; }
	if(!$limit) { $limit = "20"; }
	$edit_id = session_user_id;
	if(!$time && !$_GET['action']) {
		$time = 'past_week';
	}
	if($_GET['action'] && !$_GET['time']) {
		$time = 'all';
	}
	if($time == 'today') {
		$time_start = mktime(0, 00, 00, date('m'), date('d'), date('Y'));
		$time_end = mktime(23, 59, 59, date('m'), date('d'), date('Y'));
	} elseif($time == 'past_week') {
		$time_start = mktime(0, 00, 00, date('m'), date('d') - 6, date('Y'));
		$time_end = mktime(23, 59, 59, date('m'), date('d'), date('Y'));
	} elseif($time == 'past_2_weeks') {
		$time_start = mktime(0, 00, 00, date('m'), date('d') - 13, date('Y'));
		$time_end = mktime(23, 59, 59, date('m'), date('d'), date('Y'));
	} elseif($time == 'this_month') {
		$time_start = mktime(0, 00, 00, date('m'), 1, date('Y'));
		$time_end = mktime(23, 59, 59, date('m') + 1, 0, date('Y'));
	} elseif($time == 'past_3_months') {
		$time_start = mktime(0, 00, 00, date('m') - 2, date('d'), date('Y'));
		$time_end = mktime(23, 59, 59, date('m'), date('d'), date('Y'));
	} elseif($time == 'past_6_months') {
		$time_start = mktime(0, 00, 00, date('m') - 5, date('d'), date('Y'));
		$time_end = mktime(23, 59, 59, date('m'), date('d'), date('Y'));
	} elseif($time == 'past_12_months') {
		$time_start = mktime(0, 00, 00, date('m'), date('d'), date('Y') - 1);
		$time_end = mktime(23, 59, 59, date('m'), date('d'), date('Y'));
	} elseif($time == 'this_year') {
		$time_start = mktime(0, 00, 00, 1, 1, date('Y'));
		$time_end = mktime(23, 59, 59, 1, 0, date('Y') + 1);
	} else {
		$time_start = mktime(0,00,00,0,0,0000);
		$time_end = time() + 1000;
	}
?>
		<div id="main">
<?
	if(!drizzo_user_is_allowed('comments-moderate') || drizzo_global_options('module_comments') != 1) {
		echo "<h2>Comments</h2><p>This module is not available.</p><br class=\"clear\" />";
		include_once('footer.php');
		exit();
	}
	if($view > 0) {
		$comment = drizzo_get_the_comment($view);
?>
			<h2>View Comment</h2>
			<h3>"<? if($comment['title']) { echo $comment['title']; } else { echo "(Untitled)"; } ?>" on <em><? echo $comment['post_title']; ?></em></h3>
			<p><? echo $comment['content']; ?></p>
			<p>Tags: <? echo $comment['tags']; ?> | Posted on <? echo date('M d, Y \a\t g:i A', $comment['date']); ?></p>
			<form action="<? echo $_SERVER['HTTP_REFERER']; ?>" method="post" name="choices" id="choices">
				<p class="center"><input type="button" onclick="history.go(-1)" value="Go Back" class="submit" /> <input type="button" class="submit" name="edit" value="Edit" onclick="location = 'comments.php?edit=<? echo $comment['ID']; ?>'" /> <input type="button" class="submit" name="delete" value="Delete" onclick="location = 'comments.php?delete=<? echo $comment['ID']; ?>'" /> <? if($comment['status'] != 'spam') { ?><input type="submit" class="submit" name="spam" value="Mark as Spam" onclick="document.choices.process.value = 'spam'" /> <? } ?><input type="submit" class="submit" name="Approve" value="Approve" onclick="document.choices.process.value = 'approve'" /> </p>
				<input type="hidden" name="batch_id[]" value="<? echo $comment['ID']; ?>" /><input type="hidden" id="process" name="process" value="" />
			</form>
		<br class="clear" />
		
	</div>
<?
		include('footer.php');
		die();
	} elseif($delete > 0) {
		if($_POST['delete_check']) {
			$delete_query = @mysql_query("DELETE FROM `$drizzo_comments` WHERE `id`='$delete'");
			if(!mysql_errno()) {
?>
			<p class="trash">Comment has been Deleted.</p>
<?
			} else {
?>
			<p class="alert">Comment could not be Deleted.</p>
<?
			}
		} else {
			$comment = drizzo_get_the_comment($delete);
?>
			<h2>Delete Comment</h2>
			<form name="deleter" id="deleter" method="post" action="">
				<h2><? echo $comment['title']; ?></h2>
				<p><? echo $comment['content']; ?></p>
				<p>By <? echo $comment['author_name']; ?> on <? echo date('M d, Y \a\t g:i A', $comment['date']); ?></p>
				<p class="center">Are you sure you want to delete the comment?</p>
				<p class="center"><input type="hidden" name="delete_check" value="true" /><input type="button" onclick="history.go(-1)" value="No" class="submit" /> <input type="submit" value="Yes" class="submit" /></p>
			</form>
		
		<br class="clear" />
		
	</div>
<?
			include('footer.php');
			die();
		}
		$old_url = drizzo_parse_referer();
		$action = $old_url['action'];
		$offset = $old_url['offset'];
		$limit = $old_url['limit'];
		$sort = $old_url['sort'];
	} elseif($edit > 0) {
		if($_POST['edit_check']) {
			$title = drizzo_db_wrap($_POST['title']);
			$content = drizzo_db_wrap($_POST['content']);
			$tags = rtrim(drizzo_db_wrap($_POST['tags']), ', ');
			$status = $_POST['status'];
			$edit_query = @mysql_query("UPDATE `$drizzo_comments` SET `title`='$title',`content`='$content',`tags`='$tags',`status`='$status' WHERE `ID`='$edit'");
			if(!mysql_errno()) {
				drizzo_cache_destroy(drizzo_post_permalink($_POST['post_id']));
?>
				<p class="save">The edits to "<? echo drizzo_db_unwrap($title); ?>" have been saved.</p>
<?
			} else {
				?>
				<p class="alert">The edits to "<? echo drizzo_db_unwrap($title); ?>" could not be saved.</p>
				<?
			}
		} else {
			$comment = drizzo_get_the_comment($edit);
?>
			<h2>Edit Comment</h2>

			<form name="editor" id="editor_form" action="" method="post">

				<div id="editor">

					<label for="title">Title</label><input type="text" name="title" value="<? echo $comment['title']; ?>" /><br class="clear" />
					<label for="content">Content</label><textarea name="content"><? echo $comment['content']; ?></textarea><br class="clear" />
					<label for="tags">Tags <small>(Separate by Commas)</small></label><textarea name="tags" type="text" id="tags_input"><? echo $comment['tags']; if($comment['tags']) { echo ", "; } ?></textarea><br class="clear" />
					<label for="recent_tags">Recently Used Tags</label><p id="recent_tags"><? echo drizzo_recent_tags(); ?></p>
					<input type="hidden" name="post_id" value="<? echo $comment['post_id']; ?>" />
					<p class="right"><input type="hidden" name="edit_check" value="true" /><input type="button" onclick="history.go(-1)" value="Cancel" class="submit" /> <input type="submit" value="Save Edits" class="submit" /></p>

				</div>
			
			<div id="options">
				
				<? echo drizzo_comment_statuses_dropdown(); ?>
				
			</div>

			</form>

		<br class="clear" />
		
	</div>
<?	
			include('footer.php');
			die();
		}
	} elseif($process == 'delete') { // Begin Batch Delete Comments
		$batch_ids = $_POST['batch_id'];
		$approve_count = count($batch_ids);
		$process_count = 0;
		foreach($batch_ids as $id) {
			$delete_query = @mysql_query("DELETE FROM `$drizzo_comments` WHERE `id`='$id'");
			if(!mysql_errno()) {
				$process_count++;
			}
		}
		$old_url = drizzo_parse_referer();
		$action = $old_url['action'];
		$offset = $old_url['offset'];
		$limit = $old_url['limit'];
		$sort = $old_url['sort'];
?>
			<p class="trash"><? echo $process_count; ?> comment<? if($proccess_count != 1) { echo "s"; } ?> have been Deleted.</p>
<?
	} elseif($process == 'approve') { // End Batch Delete Comments; Begin Batch Approve Comments
		$batch_ids = $_POST['batch_id'];
		$process_count = 0;
		foreach($batch_ids as $id) {
			$now = time();
			$approve_query = @mysql_query("UPDATE `$drizzo_comments` SET `status`='approved',`edited_by`='$edit_id',`edited_date`='$now' WHERE `id`='$id'");
			if(!mysql_errno()) {
				$process_count++;
			}
		}
		$old_url = drizzo_parse_referer();
		$action = $old_url['action'];
		$offset = $old_url['offset'];
		$limit = $old_url['limit'];
		$sort = $old_url['sort'];
?>
			<p class="approve"><? echo $process_count; ?> comments have been Approved.</p>
<?
	} elseif($process == 'spam') { // End Batch Approve Comments; Begin Batch Mark Comments as Spam
		$batch_ids = $_POST['batch_id'];
		$approve_count = count($batch_ids);
		$process_count = 0;
		foreach($batch_ids as $id) {
			$now = time();
			$spam_query = @mysql_query("UPDATE `$drizzo_comments` SET `status`='spam',`edited_by`='$edit_id',`edited_date`='$now' WHERE `id`='$id'");
			if(!mysql_errno()) {
				$process_count++;
			}
		}
		$old_url = drizzo_parse_referer();
		$action = $old_url['action'];
		$offset = $old_url['offset'];
		$limit = $old_url['limit'];
		$sort = $old_url['sort'];
?>
			<p class="alert"><? echo $process_count; ?> comments have been marked as Spam.</p>
<?
	} else { // End Batch Mark Comments as Spam
		// Do Nothing
	} // End Process Comment Changes
	if($action == 'view_moderated' || $action == 'view_approved' || $action == 'view_spam' || $action == 'view_all') { // Show Comments by status
?>
			<h2><? echo ucwords(str_replace('view_', '', $action)); ?> Comments</h2>
<?
		echo drizzo_comment_table($action, $time_start, $time_end, $limit, $offest, $sort);
	} else { // End Show Comments by status; Begin Show Newest Comments (Default View)
?>
			<h2>Recent Comments</h2>
<?
		echo drizzo_comment_table('view_recent', $time_start, $time_end, $limit, $offset, $sort);
	}
?>
			<br class="clear" />

		</div>
<?
	include_once('footer.php');
?>
